<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../static/js/vue.js"></script>
    <link rel="stylesheet" href="../../static/css/element-ui.css">

    <script src="../../static/js/element-ui.js"></script>
    <script src="../../static/js/axios.js"></script>

</head>
<body>
<div id="information">
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="药名" prop="drugName">
                <el-input
                        v-model="queryParams.drugName"
                        placeholder="请输入药名"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>


            <el-form-item label="停用标志" prop="status">

                <el-select v-model="queryParams.status" placeholder="请选择停用标志">
                    <el-option label="停用" value="1" ></el-option>
                    <el-option label="可用" value="0" ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                >新增
                </el-button>

            </el-col>



        </el-row>
        <template>
            <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
                <el-table-column label="药名" align="center" prop="drugName"></el-table-column>
                <el-table-column label="规格信息" align="center" prop="specifications"></el-table-column>
                <el-table-column label="基本单位" align="center" prop="basicUnit"></el-table-column>
                <el-table-column label="最低库存要求" align="center" prop="minimumStock"></el-table-column>
                <el-table-column label="停用标志" align="center" >
                    <template slot-scope="scope">
                        {{scope.row.status == 0 ? "可用":"停用"}}
                    </template>

                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-edit"
                                @click="handleUpdate(scope.row)"
                        >修改
                        </el-button>
                        <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-delete"
                                @click="handleDelete(scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </template>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                style="float:right;margin-top: 20px"
                :page-sizes="[5, 10, 20, 50]"
                :page-size="5"
                v-show="total>0"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>

        <!-- 添加或修改药品信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="药名" prop="drugName">
                    <el-input v-model="form.drugName" placeholder="请输入药名"/>
                </el-form-item>
                <el-form-item label="规格信息" prop="specifications">
                    <el-input v-model="form.specifications" placeholder="请输入规格信息"/>
                </el-form-item>
                <el-form-item label="基本单位" prop="basicUnit">
                  <el-select v-model="form.basicUnit" placeholder="请选择基本单位">
                        <el-option
                                v-for="item in unitList"
                                :key="item.id"
                                :label="item.unit"
                                :value="item.id"
                                :disabled="item.status == 1"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="最低库存" prop="minimumStock">
                    <el-input v-model="form.minimumStock" placeholder="请输入最低库存要求"/>
                </el-form-item>
                <el-form-item label="停用标志">
                    <el-select v-model="form.status" placeholder="请选择停用标志">
                        <el-option label="停用" value="1" ></el-option>
                        <el-option label="可用" value="0" ></el-option>
                    </el-select>

                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</div>
</body>


<script>

    new Vue({
        el: '#information',
        data: function () {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                unitList:[],
                // 药品信息表格数据
                informationList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    drugName: null,
                    specifications: null,
                    basicUnit: null,
                    minimumStock: null,
                    status: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {}
            };
        },
        created() {
            this.getList();

            axios({
                url: '../../crud/unit/list',
                method: 'get',
                params:{}
            }).then(response => {
                this.unitList = response.data.rows;
                this.total = response.data.total;
                this.loading = false;
            });
        },
        methods: {
            /** 查询药品信息列表 */
            getList() {
                this.loading = true;
                axios({
                    url: '../../crud/information/list',
                    method: 'get',
                    params: this.queryParams
                }).then(response => {
                    this.informationList = response.data.rows;
                    this.total = response.data.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    drugName: null,
                    specifications: null,
                    basicUnit: null,
                    minimumStock: null,
                    status: null
                };

            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.queryParams = {
                    pageNum: 1,
                    pageSize: 10,
                    drugName: null,
                    specifications: null,
                    basicUnit: null,
                    minimumStock: null,
                    status: null
                }
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加药品信息";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                return axios({
                    url: '../../crud/information/' + id,
                    method: 'get'
                }).then(response => {
                    this.form = response.data.data;
                    this.form.status = String(this.form.status);
                    this.form.basicUnit = Number(this.form.basicUnit);


                    this.open = true;
                    this.title = "修改药品信息";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            axios({
                                url: '../../crud/information',
                                method: 'put',
                                data: this.form
                            }).then(response => {
                                if (response.data.code == 200) {
                                    this.$message({
                                        message: '修改成功',
                                        type: 'success'
                                    });
                                    this.open = false;
                                    this.getList();
                                } else {

                                    this.$message.error(response.data.msg);
                                }
                            });
                        } else {
                            axios({
                                url: '../../crud/information',
                                method: 'post',
                                data: this.form
                            }).then(response => {
                                if (response.data.code == 200) {
                                    this.$message({
                                        message: '新增成功',
                                        type: 'success'
                                    });
                                    this.open = false;
                                    this.getList();
                                } else {

                                    this.$message.error(response.data.msg);
                                }
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除药品信息编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return axios({
                        url: '../../crud/information/' + ids,
                        method: 'delete'
                    });
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
            handleSizeChange(val) {
                this.queryParams.pageSize = val
                this.getList();
            },
            handleCurrentChange(val) {
                this.queryParams.pageNum = val;
                this.getList();
            }
        }

    });
</script>
